#include"mcevaluate.h"
#include"mc.cpp"
#include<cmath>
#include<cstdio>
#include<ctime>
int loop = 100000;
static double value[14*14];
//static double mucount[14*14];
static double patternvalue[14*14];
static int count[14*14];
time_t start_time;
McSimulator simulator;
int rr;
double playvalue(int x, double y);
int gen_move(int *board, int* next, int* candidate, int tot, int color) {
    int totalsimulate = 0;
    int winsimulate = 0;
    srand(0);

    if (playpass) {
        candidate[tot] = POS(-1,-1);
        tot++;
    }
    if (debugMsg) {
        for (int i = 0; i < tot; i++) {
            patternvalue[i] = book.getScore(board, candidate[i], color);
//            if (patternvalue[i] > 0)
//                fprintf(stderr, "#pattern value %d %d %lf\n", I(candidate[i]), J(candidate[i]), patternvalue[i]);
        }
    }
    start_time = time(NULL);
    for (int i =0; i < tot; i++){
        count[i] = 10;
        value[i] = 0;
        for (int j = 0; j < 10; j++) {
	        simulator.refresh(board,next);
//        fprintf(stderr, "start %d %lf\n", candidate[i], value[i]);
        	value[i] += simulator.simulate(I(candidate[i]), J(candidate[i]), color);
        }
        value[i] /=10.0;
//        fprintf(stderr, "finish %d %lf\n", candidate[i], value[i]);
    }
    for (int i =0; i< loop;i++){
        double ini = sqrt(5*log(loop));
        double max = -1000000;
        int nextplay = -1;
        for (int j =0; j < tot; j++) {
            if (playvalue(j,ini) > max) {
                max = playvalue(j,ini);
                nextplay = j;
            }
        }
        simulator.refresh(board, next);
//        fprintf(stderr, "start %d %lf\n", candidate[nextplay], value[nextplay]);
        rr = simulator.simulate(I(candidate[nextplay]), J(candidate[nextplay]), color);
//        fprintf(stderr, "finish %d %lf\n", candidate[nextplay], value[nextplay]);
        winsimulate +=rr;
        value[nextplay] = (value[nextplay] * count[nextplay] + rr )/(count[nextplay]+1);

        count[nextplay]++;
        if (i %1000 ==0){
            fprintf(stderr, " %d %lf\n", candidate[nextplay], value[nextplay]);
            fprintf(stderr, "%lf\n", difftime(time(NULL), start_time));
            if (difftime(time(NULL),start_time) >= time_limit){
                totalsimulate = i;
                break;
            }
        }
    }
    
    // for (int i =0 ; i < tot; i++) {
    //     if (matchpattern(I(candidate[i]), J(candidate[i]), color, board))
    //         value[i]+=0.05;
    // }
    for (int i =0; i < tot; i++){
        fprintf(stderr, "%d %d %lf\n", candidate[i], count[i], value[i]);
    }
    if (playpass)
        tot--;
    double max = -100000000;
    int res = POS(-1,-1);
    for (int i =0; i < tot; i++){
        if(value[i] > max){
            max = value[i];
            res = candidate[i];
        }
    }
    if (usebook) {
//        clock_t x = clock();
        for (int i = 0; i < tot; i++) {
            if(value[i] > max - patternlimit) {
                double patternscore = book.getScore(board, candidate[i],color);
                value[i] += patternscore;
//                if (patternscore > 0)
//                    fprintf(stderr, "patternscore %lf\n",patternscore);
            }
        }
        max = -100000000;
        res = POS(-1,-1);
        for (int i =0; i < tot; i++){
            if(value[i] > max){
                max = value[i];
                res = candidate[i];
            }
        }
//        fprintf(stderr,"%d\n", clock()-x);
    }
    if (playpass) {
        int passvalue = value[tot];
        if (passvalue >= max+7) {
            max = passvalue;
            res = POS(-1,-1);
        }
    }
    // if (playpass) {
    //     for (int i =0; i < 100;i++) {
    //         simulator.refresh(board, next);
    //         passvalue += simulator.simulate(-1,-1,color);
    //     }
    //     passvalue /=100;
    //     if(passvalue >= max-20) {
    //         max = passvalue;
    //         res = POS(-1,-1);
    //     }
    // }
    fprintf(stderr, "%d %lf\n", res, max);
    fprintf(stderr, "%lf\n", value[tot]);
    fprintf(stderr, "%d/%d\n", winsimulate, totalsimulate);
//    fprintf(stderr, "%d %d %d %d %d\n", simulator.firsttime, simulator.secondtime,simulator.thirdtime,simulator.canmovetime, simulator.patterntime);
//    simulator.refreshtime();
    return res;
}

double playvalue(int i, double xx) {
    return value[i] + xx/sqrt(count[i]);
}


